package org.game;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class CardView extends ImageView {

	public CardView(Context context) {
		super(context);
	}

	public CardView(Context context, Card card) {
		super(context);
		setCard(card);
	}

	public void slideOut(){
		Animation translateAnimation = new TranslateAnimation(0, 0,0, -50);
		translateAnimation.setDuration(200);
		translateAnimation.setFillAfter(true);
		this.startAnimation(translateAnimation);
		this.setPrepared(true);
		translateAnimation=null;
	}
	public void slideIn(){
		Animation translateAnimation = new TranslateAnimation(0, 0,
				-50, 0);
		translateAnimation.setDuration(200);
		translateAnimation.setFillAfter(true);
		this.startAnimation(translateAnimation);
		this.setPrepared(false);
		translateAnimation=null;
	}
	public boolean isPrepared() {
		return prepared;
	}

	public void setPrepared(boolean prepared) {
		this.prepared = prepared;
	}

	public Card getCard() {
		return card;
	}

	public void setCard(Card card) {
		if(card!=null){
			this.card = card;
			int suitIndex=card.getSuit().ordinal();
			int FaceIndex=card.getFace().ordinal()+1;
			String file="card/"+suitIndex+FaceIndex+".jpg";
			setImageBitmap(getImageFromAssetsFile(file));
		}
	}

	private Bitmap getImageFromAssetsFile(String fileName) {
		Bitmap image = null;
		AssetManager am = getResources().getAssets();
		try {
			InputStream is = am.open(fileName);
			image = BitmapFactory.decodeStream(is);
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return image;
	}

	private Card card;
	private boolean prepared=false;

}
